'use strict';
import React, { createClass } from 'react'
import { connect } from 'react-redux'
import { Link } from 'react-router'
import { optionSelected } from '../actions'

const TestResult = createClass({
    render: function () {
        const { params, questionReducer, optionReducer } = this.props;
        let qid = params.id,
            totalScore = 0, // 总分数
            nullList = [],
            resultBoard = '';

        if ($.isArray(optionReducer.data) && optionReducer.data.length > 0) {
            $.each(optionReducer.data, function (k, v) {
                if (v) {
                    totalScore += v.score || 0;
                }
                else {
                    nullList.push(k + 1);
                }
            });

            let nullStr = nullList.join(' , '),
                nullNum = nullList.length;

            if (nullNum > 0) {
                resultBoard = <div className="scoreboard">
                    <h1 className="score-title">当前分数：{totalScore}</h1>
                    <div className="alert alert-info"><p>试题回答不完整，无法获取测试评估。</p><p>有{nullNum}道题({nullStr})没有做答。</p></div>
                </div>;
            }
            else {
                let clsName = '',
                    resultMsg = '',
                    rangeArr = (questionReducer && questionReducer.data && questionReducer.data.range) ? questionReducer.data.range : null;

                if ($.isArray(rangeArr)) {
                    $.each(rangeArr, function (k, range) {
                        var bArr = [];
                        if (range['lt'] != undefined) {
                            if (totalScore < range['lt']) {
                                bArr.push(true);
                            }
                            else {
                                bArr.push(false);
                            }
                        }
                        if (range['egt'] != undefined) {
                            if (totalScore >= range['egt']) {
                                bArr.push(true);
                            } else {
                                bArr.push(false);
                            }
                        }

                        if (bArr.length > 0) {
                            var cnt = 0;
                            $.each(bArr, function (i, val) {
                                if (val) {
                                    cnt++;
                                }
                            });
                            if (bArr.length == cnt) {
                                let getResultMsg = function () {
                                    return {__html: range.evaluate};
                                };
                                resultMsg = <div dangerouslySetInnerHTML={getResultMsg()}/>;
                                if(range.className) {
                                    clsName = range.className;
                                }
                                return false;
                            }
                        }
                    });
                }

                resultBoard = <div className="scoreboard">
                    <h1 className="score-title">测试结果为 {totalScore}</h1>
                    <div className={clsName}>{resultMsg}</div>
                </div>;
            }
        }
        else {
            resultBoard = <div className="scoreboard">
                <div className="alert alert-info">您还没有进行测试。</div>
            </div>;
        }

        let linkTo = {pathname: '/test_paper/' + qid};

        return <div className="app-container">
            {resultBoard}
            <Link className="btn btn-primary" to={linkTo}>返回</Link>
        </div>
    }
});

const mapStateToProps = (state) => {
    const { questionReducer, optionReducer } = state;
    return {
        questionReducer,
        optionReducer
    }
};
export default connect(mapStateToProps)(TestResult);